//Date: 2024-06-30 21:53:15
//LastEditors: liangzhengjie 786784022@qq.com
//LastEditTime: 2024-07-04 22:32:39
//FilePath: /photowall/pkg/optiondef/db_interface.go
// 这个文件定义数据库的相关操作，所有的持久化数据库操作都应该使用这个文件的接口进行调用

package optiondef

import (
	"fmt"
	"photowall/pkg/log"
	"sync"
)

type WxUserInfo struct {
	ID          string `db:"id" yaml:"id" bson:"id"`
	OpenID      string `db:"open_id" yaml:"open_id" bson:"open_id"`
	UnionID     string `db:"union_id" yaml:"union_id" bson:"union_id"`
	UserName    string `db:"user_name" yaml:"user_name" bson:"user_name"`
	PhoneNumber string `db:"phone_number" yaml:"phone_number" bson:"phone_number"`
	Password    string `db:"password" yaml:"password" bson:"password"`
	SessionKey  string `db:"session_key" yaml:"session_key" bson:"session_key"`
}

type WxuserDbOptions interface {
	AddWxUserToDb(info *WxUserInfo) (res bool, err error)
	IsWxUserInDb(info *WxUserInfo) (res bool, err error)
	DeleteWxUserInDb(info *WxUserInfo) (res bool, err error)
	GetWxUserInDb(info *WxUserInfo) (userinfo *WxUserInfo, err error)
	UpdateWxUserInDb(info *WxUserInfo) (res bool, err error)
}

type userDbService struct {
	WxuserDbOptions
}

var (
	instance *userDbService
	once     sync.Once
)

func getWxUserdbService() WxuserDbOptions {
	once.Do(func() {
		instance = &userDbService{}
	})

	return instance
}

func (userService *userDbService) AddWxUserToDb(info *WxUserInfo) (bool, error) {
	res, err := addWechatUserInMongodb(info)
	if err != nil {
		log.GetDatabaseLogger().Error(fmt.Sprintf(`AddUserToDb failed, %v`, *info))
		return false, err
	}

	if !res {
		return false, fmt.Errorf(`AddUserToDb failed but no err, %v`, info)
	}

	return true, nil

}

func (userService *userDbService) DeleteWxUserInDb(info *WxUserInfo) (bool, error) {
	res, err := deleteWechatUserInMongodb(info.OpenID)
	if err != nil {
		log.GetDatabaseLogger().Error(fmt.Sprintf(`DeleteUserInDb failed, %v`, *info))
		return false, err
	}

	if !res {
		log.GetDatabaseLogger().Info(fmt.Sprintf(`DeleteUserInDb no one deleted, %v`, *info))
		return false, nil
	}

	return true, nil
}

func (userService *userDbService) UpdateWxUserInDb(info *WxUserInfo) (bool, error) {

	res, err := updateWechatUserInMongodb(info)
	if err != nil {
		log.GetDatabaseLogger().Error(fmt.Sprintf(`UpdateUserInDb failed, %v`, *info))
		return false, err
	}

	if !res {
		log.GetDatabaseLogger().Info(fmt.Sprintf(`UpdateUserInDb failed and no one in it,  %v`, *info))
		return false, nil
	}

	return true, nil

}

func (userService *userDbService) GetWxUserInDb(info *WxUserInfo) (*WxUserInfo, error) {
	res, err := getWechatUserByOpenIDInMongodb(info.OpenID)
	if err != nil {
		log.GetDatabaseLogger().Error(fmt.Sprintf(`IsUserInDb failed, %v`, *info))
		return nil, err
	}

	if res == nil {
		return nil, nil
	}

	return res, nil

}

func (userService *userDbService) IsWxUserInDb(info *WxUserInfo) (bool, error) {
	res, err := getWechatUserByOpenIDInMongodb(info.OpenID)
	if err != nil {
		log.GetDatabaseLogger().Error(fmt.Sprintf(`IsUserInDb failed, %v, err:%v`, *info, err.Error()))
		return false, err
	}

	if res == nil {
		log.GetDatabaseLogger().Info(fmt.Sprintf(`IsWxUserInDb getuser failed, %v, opend_id is %v`, *info, info.OpenID))
		return false, nil
	}

	return true, nil
}
